<?php

//
// +---------------------------------------------------------+
// | 开发组
// +---------------------------------------------------------+
// | Copyrigh  (c)  2020 The PHP Group
// +---------------------------------------------------------+
// | 商户 代理商
// +---------------------------------------------------------+
// | zxc
// | php后端开发
// |
// +———————————————————+
//

namespace App\Models\Api;

use App\Tools\Financial;
use App\Tools\Prstatic;
use Illuminate\Database\Eloquent\Model;

class Store extends Model
{

    protected $table = 'store';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U'; // 时间戳

    /**
     * 获取信息
     * @param $id
     */
    public static function getInfoById($id)
    {
        $info = self::where(['id' => $id])->first();
        if ($info) {
            $info['agent_start_time'] = date('Y-m-d', $info['agent_start_time']);
            $info['agent_end_time'] = date('Y-m-d', $info['agent_end_time']);
            return $info->toArray();
        }
        return [];
    }

    /**
     * 创建代理商
     * @param $data
     */
    public static function addInfo($data)
    {
        $info['store_sn'] = $data['store_sn'];
        $info['store_name'] = $data['store_name'];
        $info['money_goods'] = $data['money_goods'] ?: 0;
        $info['money_management'] = $data['money_management'] ?: 0;
        $info['principal_name'] = $data['principal_name'];
        $info['principal_mobile'] = $data['principal_mobile'];
        $info['principal_email'] = $data['principal_email'];
        $info['principal_address'] = $data['principal_address'];
        $info['principal_card_sn'] = $data['principal_card_sn'];
        $info['principal_card_a'] = $data['principal_card_a'];
        $info['principal_card_b'] = $data['principal_card_b'];
        $info['company_sn'] = $data['company_sn'];
        $info['company_license'] = $data['company_license'];
        $info['company_license_yy'] = $data['company_license_yy'] ?: '';
        $info['company_type'] = $data['company_type'];
        $info['company_name'] = $data['company_name'];
        $info['company_addrss'] = $data['company_addrss'];
        $info['company_corporation'] = $data['company_corporation'];
        $info['company_registered_capital'] = $data['company_registered_capital'];
        $info['company_create_time'] = $data['company_create_time'];
        $info['company_operating_period'] = $data['company_operating_period'];
        $info['company_business_scope'] = $data['company_business_scope'];
        $info['is_replenishment'] = $data['is_replenishment'] ?: 1;
        $info['discount_rate'] = $data['discount_rate'] ?: 1;
        $info['is_express_fee'] = $data['is_express_fee'] ?: 1;
        $info['is_agent'] = $data['is_agent'] ?: 0;
        $info['remark'] = $data['remark'] ?: '';
        $info['status'] = $data['status'] ?: 3;
        $info['created_at'] = time();
        $info['apply_time'] = time();
        $info['agent_start_time'] = $data['agent_start_time'];
        $info['agent_end_time'] = $data['agent_end_time'];
        return self::insertGetId(array_filter($info));
    }

    /**
     * 修改信息
     * @param $id
     * @param $data
     */
    public static function editInfoBySId($store_id, $data)
    {
        $info['principal_email'] = $data['principal_email'] ?: '';
        $info['principal_address'] = $data['principal_address'] ?: '';
        $info['company_addrss'] = $data['company_addrss'] ?: '';
        return self::where(['id' => $store_id])->update($info);
    }
}
